﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;

public partial class Views_EmployeesProfile : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            StorecbxAddManaProvince_LoadData();
        }
    }

    protected void StorecbxAddManaProvince_LoadData()
    {
        try
        {
            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                List<Province> list = db.Provinces.ToList<Province>();
                list.Sort((new SortObjectsProvinces()).Compare);

                StorecbxAddManaProvince.DataSource = list;
                StorecbxAddManaProvince.DataBind();
            }
            ViewInfor_Reset();
        }
        catch (Exception)
        {
            X.Msg.Alert("Hệ thống thông báo", "Hệ thống đang bận, hãy thử lại sau");
        }
    }

    [DirectMethod]
    public void cbxAddManaProvince_SelectedChange()
    {
        try
        {
            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                int provID = Int32.Parse(cbxAddManaProvince.Value.ToString());
                Province provSelected = db.Provinces.Single(p => p.ProvID == provID);

                List<District> list = provSelected.Districts.ToList<District>();
                StoreViewDistrictByProvinces.DataSource = list;
                StoreViewDistrictByProvinces.DataBind();


                StoreAddManaCommune.DataSource = new List<Commune>();
                StoreAddManaCommune.DataBind();

                StoreAddManaOrganization.DataSource = new List<Organization>();
                StoreAddManaOrganization.DataBind();

                StoreAddManaDepartment.DataSource = new List<Department>();
                StoreAddManaDepartment.DataBind();

                StorecbxUpdateForEmpProfiles.DataSource = new List<Employee>();
                StorecbxUpdateForEmpProfiles.DataBind();
            }
            ViewInfor_Reset();
        }
        catch (Exception)
        {
            X.Msg.Alert("Hệ thống thông báo", "Hệ thống đang bận, hãy thử lại sau");
        }
    }

    [DirectMethod]
    public void cbxAddManaDistrict_SelectedChange()
    {
        try
        {
            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                int distID = Int32.Parse(cbxAddManaDistrict.Value.ToString());

                District dist = db.Districts.Single(p => p.DistID == distID);

                StoreAddManaCommune.DataSource = dist.Communes.ToList<Commune>();
                StoreAddManaCommune.DataBind();

                StoreAddManaOrganization.DataSource = new List<Organization>();
                StoreAddManaOrganization.DataBind();

                StoreAddManaDepartment.DataSource = new List<Department>();
                StoreAddManaDepartment.DataBind();

                StorecbxUpdateForEmpProfiles.DataSource = new List<Employee>();
                StorecbxUpdateForEmpProfiles.DataBind();
            }
            ViewInfor_Reset();
        }
        catch (Exception)
        {
            X.Msg.Alert("Hệ thống thông báo", "Hệ thống đang bận, hãy thử lại sau");
        }
    }

    [DirectMethod]
    public void cbxAddManaCommune_SelectedChange()
    {
        try
        {
            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                int comID = Int32.Parse(cbxAddManaCommune.Value.ToString());

                Commune dist = db.Communes.Single(c => c.ComID == comID);

                StoreAddManaOrganization.DataSource = dist.Organizations.ToList<Organization>();
                StoreAddManaOrganization.DataBind();

                StoreAddManaDepartment.DataSource = new List<Department>();
                StoreAddManaDepartment.DataBind();

                StorecbxUpdateForEmpProfiles.DataSource = new List<Employee>();
                StorecbxUpdateForEmpProfiles.DataBind();
            }
            ViewInfor_Reset();
        }
        catch (Exception)
        {
            X.Msg.Alert("Hệ thống thông báo", "Hệ thống đang bận, hãy thử lại sau");
        }
    }

    [DirectMethod]
    public void cbxAddManaOrganization_SelectedChange()
    {
        try
        {
            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                int orgID = Int32.Parse(cbxAddManaOrganization.Value.ToString());

                Organization org = db.Organizations.Single(o => o.OrgID == orgID);

                StoreAddManaDepartment.DataSource = org.Departments.ToList<Department>();
                StoreAddManaDepartment.DataBind();

                StorecbxUpdateForEmpProfiles.DataSource = new List<Employee>();
                StorecbxUpdateForEmpProfiles.DataBind();
            }
            ViewInfor_Reset();
        }
        catch (Exception)
        {
            X.Msg.Alert("Hệ thống thông báo", "Hệ thống đang bận, hãy thử lại sau");
        }
    }

    [DirectMethod]
    public void cbxAddManaDepartment_SelectedChange()
    {
        try
        {
            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                int depID = Int32.Parse(cbxAddManaDepartment.Value.ToString());

                Department dep = db.Departments.Single(o => o.DepID == depID);

                StorecbxUpdateForEmpProfiles.DataSource = dep.Employees1.ToList<Employee>();
                StorecbxUpdateForEmpProfiles.DataBind();
            }
            ViewInfor_Reset();
        }
        catch (Exception)
        {
            X.Msg.Alert("Hệ thống thông báo", "Hệ thống đang bận, hãy thử lại sau");
        }
    }

    [DirectMethod]
    public void cbxUpdateForEmpProfiles_SelectedChange()
    {
        ViewInfor_Reset();
        try
        {
            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                int empID = Int32.Parse(cbxUpdateForEmpProfiles.Value.ToString());
                Profily profileSelected = db.Profilies.Single(p => p.EmpID == empID);

                ImageAvatarView.ImageUrl = "Avatars/" + profileSelected.Employee.AvatarUrl;

                DisCommunistPartyOfDate.Text = DateTime.Parse(profileSelected.CommunistPartyOfDate.ToString()).ToString("dd/MM/yyyy");
                DisCommunistPartyOfFormally.Text = DateTime.Parse(profileSelected.CommunistPartyOfFormally.ToString()).ToString("dd/MM/yyyy");
                DisUnionsOfDate.Text = DateTime.Parse(profileSelected.UnionsOfDate.ToString()).ToString("dd/MM/yyyy");
                DisJoinTheArmy.Text = DateTime.Parse(profileSelected.JoinTheArmy.ToString()).ToString("dd/MM/yyyy");
                DisDemobilized.Text = DateTime.Parse(profileSelected.Demobilized.ToString()).ToString("dd/MM/yyyy");
                DisHighestOfRank.Text = profileSelected.HighestOfRank;
                DisHighestOfHonors.Text = profileSelected.HighestOfHonors;


                DisForteOfWork.Text = profileSelected.ForteOfWork;
                DisCommendation.Text = profileSelected.Commendation;
                DisDisciplined.Text = profileSelected.Disciplined;
                DisStateOfHealth.Text = profileSelected.StateOfHealth;
                DisHeight.Text = profileSelected.Height.ToString();
                DisWeight.Text = profileSelected.Weight.ToString();
                DisBloodOfGroup.Text = profileSelected.BloodOfGroup;
                DisWarInvalids.Text = profileSelected.WarInvalids;
                DisPolicyOfFamily.Text = profileSelected.PolicyOfFamily;


                DisIdentityCards.Text = profileSelected.IdentityCards;
                DisIdentityCardsOfDate.Text = DateTime.Parse(profileSelected.IdentityCardsOfDate.ToString()).ToString("dd/MM/yyyy");
                DisPlaceOfCard.Text = profileSelected.PlaceOfCard;
                DisNumberOfInsurance.Text = profileSelected.NumberOfInsurance;


                DisDescriptions.Text = profileSelected.Descriptions;
                DisReviewedAndEvaluation.Text = profileSelected.ReviewedAndEvaluation;
            }
        }
        catch (Exception)
        {
            X.Msg.Alert("Hệ thống thông báo", "Hệ thống đang bận, hãy thử lại sau");
        }
    }

    protected void ButtonUpdateProfiles_DirectClick(object sender, Ext.Net.DirectEventArgs e)
    {
        try
        {
            if (cbxUpdateForEmpProfiles.Value.ToString().Equals(""))
            {
                X.Msg.Alert("Hệ thống thông báo", "Bạn chưa chọn cán bộ nhân viên nào để cập nhật thông tin cá nhân").Show();
                cbxUpdateForEmpProfiles.Focus(true, 100);
                return;
            }
            WindowUpdateProfiles.Hidden = false;

            int empID = Int32.Parse(cbxUpdateForEmpProfiles.Value.ToString());
            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                Profily proSelected = db.Profilies.Single(p => p.EmpID == empID);


                TextCommunistPartyOfDate.Text = proSelected.CommunistPartyOfDate.ToString();

                TextCommunistPartyOfFormally.Text = proSelected.CommunistPartyOfFormally.ToString();
                DateUnionsOfDate.Text = proSelected.UnionsOfDate.ToString();
                DateJoinTheArmy.Text = proSelected.JoinTheArmy.ToString();
                DateDemobilized.Text = proSelected.Demobilized.ToString();
                DateFHighestOfRank.Text = proSelected.HighestOfRank;
                DateHighestOfHonors.Text = proSelected.HighestOfHonors;


                TextIdentityCards.Text = proSelected.IdentityCards;
                TextIdentityCardsOfDate.Text = proSelected.IdentityCardsOfDate.ToString();
                TextPlaceOfCard.Text = proSelected.PlaceOfCard;
                TextNumberOfInsurance.Text = proSelected.NumberOfInsurance;


                TextForteOfWork.Text = proSelected.ForteOfWork;
                TextCommendation.Text = proSelected.Commendation;
                TextDisciplined.Text = proSelected.Disciplined;


                TextStateOfHealth.Text = proSelected.StateOfHealth;
                TextBloodOfGroup.Text = proSelected.BloodOfGroup;


                TextHeight.Text = proSelected.Height.ToString();
                TextWeight.Text = proSelected.Weight.ToString();
                TextWarInvalids.Text = proSelected.WarInvalids;
                TextPolicyOfFamily.Text = proSelected.PolicyOfFamily;


                TextDescriptions.Text = proSelected.Descriptions;
                TextReviewedAndEvaluation.Text = proSelected.ReviewedAndEvaluation;
            }
        }
        catch (Exception)
        {
            X.Msg.Alert("Hệ thống thông báo", "Hệ thống đang bận, hãy thử lại sau").Show();
        }
    }

    protected void ViewInfor_Reset()
    {
        DisCommunistPartyOfDate.Text = "";
        DisCommunistPartyOfFormally.Text = "";
        DisUnionsOfDate.Text = "";
        DisJoinTheArmy.Text = "";
        DisDemobilized.Text = "";
        DisHighestOfRank.Text = "";
        DisHighestOfHonors.Text = "";


        DisForteOfWork.Text = "";
        DisCommendation.Text = "";
        DisDisciplined.Text = "";
        DisStateOfHealth.Text = "";
        DisHeight.Text = "";
        DisWeight.Text = "";
        DisBloodOfGroup.Text = "";
        DisWarInvalids.Text = "";
        DisPolicyOfFamily.Text = "";


        DisIdentityCards.Text = "";
        DisIdentityCardsOfDate.Text = "";
        DisPlaceOfCard.Text = "";
        DisNumberOfInsurance.Text = "";


        DisDescriptions.Text = "";
        DisReviewedAndEvaluation.Text = "";
    }

    [DirectMethod]
    public void UpdateProfiles_DirectClick()
    {
        try
        {
            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                int empID = Int32.Parse(cbxUpdateForEmpProfiles.Value.ToString());
                Employee empSelected = db.Employees.Single(e => e.EmpID == empID);
                Profily profileSelected = db.Profilies.Single(p => p.EmpID == empID);


                profileSelected.CommunistPartyOfDate = DateTime.Parse(TextCommunistPartyOfDate.Text.Trim());
                profileSelected.CommunistPartyOfFormally = DateTime.Parse(TextCommunistPartyOfFormally.Text.Trim());
                profileSelected.UnionsOfDate = DateTime.Parse(DateUnionsOfDate.Text.Trim());
                profileSelected.JoinTheArmy = DateTime.Parse(DateJoinTheArmy.Text.Trim());
                profileSelected.Demobilized = DateTime.Parse(DateDemobilized.Text.Trim());

                profileSelected.HighestOfRank = DateFHighestOfRank.Text.Trim();
                profileSelected.HighestOfHonors = DateHighestOfHonors.Text.Trim();


                profileSelected.ForteOfWork = TextForteOfWork.Text.Trim();
                profileSelected.Commendation = TextCommendation.Text.Trim();
                profileSelected.Disciplined = TextDisciplined.Text.Trim();
                profileSelected.StateOfHealth = TextStateOfHealth.Text.Trim();
                profileSelected.Height = float.Parse(TextHeight.Text.Trim());
                profileSelected.Weight = float.Parse(TextWeight.Text.Trim());

                profileSelected.BloodOfGroup = TextBloodOfGroup.Text.Trim();
                profileSelected.WarInvalids = TextWarInvalids.Text.Trim();
                profileSelected.PolicyOfFamily = TextPolicyOfFamily.Text.Trim();


                profileSelected.IdentityCards = TextIdentityCards.Text.Trim();
                profileSelected.PlaceOfCard = TextPlaceOfCard.Text.Trim();
                profileSelected.NumberOfInsurance = TextNumberOfInsurance.Text.Trim();
                profileSelected.IdentityCardsOfDate = DateTime.Parse(TextIdentityCardsOfDate.Text.Trim());

                profileSelected.Descriptions = TextDescriptions.Text.Trim();
                profileSelected.ReviewedAndEvaluation = TextReviewedAndEvaluation.Text.Trim();

                db.SaveChanges();
                WindowUpdateProfiles.Hidden = true;



                // View info wwhen update success
                DisCommunistPartyOfDate.Text = DateTime.Parse(profileSelected.CommunistPartyOfDate.ToString()).ToString("dd/MM/yyyy");
                DisCommunistPartyOfFormally.Text = DateTime.Parse(profileSelected.CommunistPartyOfFormally.ToString()).ToString("dd/MM/yyyy");
                DisUnionsOfDate.Text = DateTime.Parse(profileSelected.UnionsOfDate.ToString()).ToString("dd/MM/yyyy");
                DisJoinTheArmy.Text = DateTime.Parse(profileSelected.JoinTheArmy.ToString()).ToString("dd/MM/yyyy");
                DisDemobilized.Text = DateTime.Parse(profileSelected.Demobilized.ToString()).ToString("dd/MM/yyyy");
                DisHighestOfRank.Text = profileSelected.HighestOfRank;
                DisHighestOfHonors.Text = profileSelected.HighestOfHonors;


                DisForteOfWork.Text = profileSelected.ForteOfWork;
                DisCommendation.Text = profileSelected.Commendation;
                DisDisciplined.Text = profileSelected.Disciplined;
                DisStateOfHealth.Text = profileSelected.StateOfHealth;
                DisHeight.Text = profileSelected.Height.ToString();
                DisWeight.Text = profileSelected.Weight.ToString();
                DisBloodOfGroup.Text = profileSelected.BloodOfGroup;
                DisWarInvalids.Text = profileSelected.WarInvalids;
                DisPolicyOfFamily.Text = profileSelected.PolicyOfFamily;


                DisIdentityCards.Text = profileSelected.IdentityCards;
                DisIdentityCardsOfDate.Text = DateTime.Parse(profileSelected.IdentityCardsOfDate.ToString()).ToString("dd/MM/yyyy");
                DisPlaceOfCard.Text = profileSelected.PlaceOfCard;
                DisNumberOfInsurance.Text = profileSelected.NumberOfInsurance;


                DisDescriptions.Text = profileSelected.Descriptions;
                DisReviewedAndEvaluation.Text = profileSelected.ReviewedAndEvaluation;
                X.Msg.Alert("Thông báo hệ thống", "Cập nhật thông tin cá nhân thành công").Show();
            }
        }
        catch (Exception)
        {
            X.Msg.Alert("Thông báo hệ thống", "Hệ thống đang bận, hãy thử lại sau").Show();
        }
    }
}